package Middlewares

import (
	"fmt"
	"gin/CloudRestaurant/tool"
	"github.com/gin-gonic/gin"
	"time"
)

// 验证是否含有token
func JWTMiddleware() gin.HandlerFunc  {
	return func(context *gin.Context) {
		fmt.Println("执行中间件")
		var code= true
		//fmt.Println(context.Request.Header.Get("token"))
		token := context.Request.Header.Get("token")
		if token == ""{
			code = false
		}else {
			claims,err := tool.ParseToken(token)
			if err != nil{
				code = false
			}else if time.Now().Unix()> claims.ExpiresAt{
				code = false
			}
		}


		// 如果code 等于false 说明出现bug
		if code != true {
			tool.Success(context,"没有token获得token失效")
			context.Abort()
			return
		}
		context.Next()




	}
}